$include("$theme/entryDetailView.css");

$import("qp.jq.Scrollable");

with ($ns("qp.controls"))
{
    qp.controls.EntryDetailView = function($p_container, p_options)
    {
        var me = new mx.controls.JQControl($p_container, p_options);
        var base = {};
        
        me.entry = null;
        
        me.init = function()
        {
            me.$container.addClass("entryDetailView");
            
            me.$("#attributes").verticalScrollable({ height: 285 });

            me.$("#viewInDouban").click(function(e) {
                me.viewInDouban();
            });
        };
        
        me.loadEntry = function(p_entry)
        {
            me.entry = p_entry;
            
            
            DOUBAN.getMovie({
                id: me.entry.nid,
                callback: _getMovie_callback
            });
            
            
            // Basic
            me.$("#title_cn").text(me.entry.title_cn);
            me.$("#title").text(me.entry.title);
            
            
            // Rating
            var stars = Math.floor(me.entry.rating.average + 0.5) / 2;            
            var $stars = me.$("#rating > img");
            for (var i = 1; i <= 5; i++)
            {
                var $star = $stars.eq(i - 1);
                if (stars >= i)
                {
                    $star.attr("src", "icons/star.png");
                }
                else if (stars >= i - 0.5)
                {
                    $star.attr("src", "icons/half_star.png");
                }
                else
                {
                    $star.attr("src", "icons/no_star.png");
                }
            }
            me.$("#rating > #ratingSummary").text("(" + $format(me.entry.rating.average, "0.0") + " 分, " + me.entry.rating.numRaters + " 人评价)");
            
            
            
            // Attributes            
            var attr = me.entry.attribute;
            if (attr.director)
            {
                for (var i = 0; i < attr.director.length; i++)
                {
                    if (i != 0)
                    {
                        var $span = $("<span> / </span>");
                        me.$("#director").append($span);
                    }
                    var $link = $("<a target='_blank'/>");
                    var t = attr.director[i];
                    $link.attr("href", "http://movie.douban.com/subject_search?search_text=" + encodeURIComponent(t));
                    $link.text(t);
                    me.$("#director").append($link);
                }
            }
            if (attr.writer)
            {
                for (var i = 0; i < attr.writer.length; i++)
                {
                    if (i != 0)
                    {
                        var $span = $("<span> / </span>");
                        me.$("#writer").append($span);
                    }
                    var $link = $("<a target='_blank'/>");
                    var t = attr.writer[i];
                    $link.attr("href", "http://movie.douban.com/subject_search?search_text=" + encodeURIComponent(t));
                    $link.text(t);
                    me.$("#writer").append($link);
                }
            }
            if (attr.cast)
            {
                for (var i = 0; i < attr.cast.length; i++)
                {
                    if (i != 0)
                    {
                        var $span = $("<span> / </span>");
                        me.$("#cast").append($span);
                    }
                    var $link = $("<a target='_blank'/>");
                    var t = attr.cast[i];
                    $link.attr("href", "http://movie.douban.com/subject_search?search_text=" + encodeURIComponent(t));
                    $link.text(t);
                    me.$("#cast").append($link);
                }
            }
            if (attr.country)
            {
                me.$("#country").text(attr.country.join(" / "));
            }
            if (attr.language)
            {
                me.$("#language").text(attr.language.join(" / "));
            }
            
            try
            {
                me.$("#pubdate").text($format(parseDate(attr.pubdate[0]), "yyyy年M月d日"));
            }
            catch (e) { }
            
            me.$container.fadeIn();
            me.$("#attributes").updateScrollbars();
        };
        
        me.viewInDouban = function()
        {
            if (me.entry == null)
            {
                window.open("http://movie.douban.com/");
            }
            else
            {
                window.open(me.entry.link.alternate);
            }
        };
        
        
        
        function _getMovie_callback(p_result)
        {
            me.entry = DOUBAN.parseSubject(p_result);
            console.log(me.entry);
            
            me.$("#summary").text(me.entry.summary);
            me.$("#attributes").updateScrollbars();
        }
        
        
        
        
        
        
        return me;
    }
}